<?php
class Utils {
    public static function dump($obj, $what = NULL) {
        if (defined ( 'DEBUG_ENABLED' ) && ! DEBUG_ENABLED) {
            return;
        }
        echo "<pre>\n";
        if (is_null ( $obj )) {
            echo ($what !== NULL) ? "$what=!NULL!" : '!NULL!';
        } else if ($obj === '') {
            echo ($what !== NULL) ? "$what=!EMPTY!" : '!EMPTY!';
        } else if ($obj === false) {
            echo ($what !== NULL) ? "$what=!FALSE!" : '!FALSE!';
        } else {
            if ($what !== NULL) {
                echo "$what=<br>";
            }
            echo htmlspecialchars ( print_r ( $obj, true ) );
        }
        echo "</pre>\n";
    }
    public static function getParam($itm, $default = '', $trimText = false) {
        if (isset ( $_POST [$itm] )) {
            return urldecode ( ($trimText) ? trim ( $_POST [$itm] ) : $_POST [$itm] );
        } else if (isset ( $_GET [$itm] )) {
            return urldecode ( ($trimText) ? trim ( $_GET [$itm] ) : $_GET [$itm] );
        } else {
            return urldecode ( $default );
        }
    }
    public static function getUrl() {
        $pageURL = 'http';
        if (isset ( $_SERVER ["HTTPS"] ) && $_SERVER ["HTTPS"] == "on") {
            $pageURL .= "s";
        }
        $pageURL .= "://";
        if ($_SERVER ["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER ["SERVER_NAME"] . ":" . $_SERVER ["SERVER_PORT"] . $_SERVER ["REQUEST_URI"];
        } else {
            $pageURL .= $_SERVER ["SERVER_NAME"] . $_SERVER ["REQUEST_URI"];
        }
        return $pageURL;
    }
    public static function getShort($str, $num) {
        if (strlen ( $str ) <= $num) {
            return $str;
        }
        return mb_substr ( $str, 0, $num, 'utf-8' ) . '...';
    }
    public static function convert($str, $ky = '') {
        if ($ky == '')
            return $str;
        $ky = str_replace ( chr ( 32 ), '', $ky );
        if (strlen ( $ky ) < 8)
            exit ( 'key error' );
        $kl = strlen ( $ky ) < 32 ? strlen ( $ky ) : 32;
        $k = array ();
        for($i = 0; $i < $kl; $i ++) {
            $k [$i] = ord ( $ky {$i} ) & 0x1F;
        }
        $j = 0;
        for($i = 0; $i < strlen ( $str ); $i ++) {
            $e = ord ( $str {$i} );
            $str {$i} = $e & 0xE0 ? chr ( $e ^ $k [$j] ) : chr ( $e );
            $j ++;
            $j = $j == $kl ? 0 : $j;
        }
        return $str;
    }
    public static function rrmdir($dir) {
        if (is_dir ( $dir )) {
            $objects = scandir ( $dir );
            foreach ( $objects as $object ) {
                if ($object != "." && $object != "..") {
                    if (filetype ( $dir . "/" . $object ) == "dir") {
                        Utils::rrmdir ( $dir . "/" . $object );
                    } else {
                        unlink ( $dir . "/" . $object );
                    }
                }
            }
            reset ( $objects );
            rmdir ( $dir );
        }
    }
    public static function create($dir) {
        if (! is_dir ( $dir )) {
            $temp = explode ( '/', $dir );
            $cur_dir = '';
            for($i = 0; $i < count ( $temp ); $i ++) {
                $cur_dir .= $temp [$i] . '/';
                if (! is_dir ( $cur_dir )) {
                    @mkdir ( $cur_dir, 0777 );
                }
            }
        }
    }
    public static function unique($length = 32) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for($i = 0; $i < $length; $i ++) {
            $randomString .= $characters [rand ( 0, strlen ( $characters ) - 1 )];
        }
        return $randomString;
    }
    public static function getIp() {
        if (getenv ( "HTTP_CLIENT_IP" ) && strcasecmp ( getenv ( "HTTP_CLIENT_IP" ), "unknown" )) {
            $ip = getenv ( "HTTP_CLIENT_IP" );
        } else if (getenv ( "HTTP_X_FORWARDED_FOR" ) && strcasecmp ( getenv ( "HTTP_X_FORWARDED_FOR" ), "unknown" )) {
            $ip = getenv ( "HTTP_X_FORWARDED_FOR" );
        } else if (getenv ( "REMOTE_ADDR" ) && strcasecmp ( getenv ( "REMOTE_ADDR" ), "unknown" )) {
            $ip = getenv ( "REMOTE_ADDR" );
        } else if (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], "unknown" )) {
            $ip = $_SERVER ['REMOTE_ADDR'];
        } else {
            $ip = "unknown";
        }
        return ($ip);
    }
    public static function distance($lat1, $lon1, $lat2, $lon2, $unit) {
        $theta = $lon1 - $lon2;
        $dist = sin ( deg2rad ( $lat1 ) ) * sin ( deg2rad ( $lat2 ) ) + cos ( deg2rad ( $lat1 ) ) * cos ( deg2rad ( $lat2 ) ) * cos ( deg2rad ( $theta ) );
        $dist = acos ( $dist );
        $dist = rad2deg ( $dist );
        $miles = $dist * 60 * 1.1515;
        $unit = strtoupper ( $unit );
        
        if ($unit == "K") {
            return ($miles * 1.609344);
        } else if ($unit == "N") {
            return ($miles * 0.8684);
        } else {
            return $miles;
        }
    }
}